//
//  MainViewController.swift
//  weibo
//
//  Created by 张宏芝 on 15/8/31.
//  Copyright © 2015年 kama. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func loadView() {
        super.loadView()
    }
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        addChildViewControllers()
    }
    
//    视图将要出现的时候,tabBar才会被加载
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        setupButton()
    }
    
//    添加所有的子控制器
    private func addChildViewControllers() {
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
         addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        
//        "+"按钮设置
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
         addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
//    添加单独的子控制器
    private func addChildViewController(vc: UIViewController,title:String,imageName:String) {

// 设置标题 vc.navigationItem.title = title 与  vc.tabBarItem.title = title 就等于
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: imageName)
//        将每一个子控制器包装成导航控制器
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
    }
    
//    "+"按钮(懒加载)
    private lazy var addButton: UIButton =  {
        
        let btn = UIButton()
       
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
       
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
//        将button添加到tabBar上
        self.tabBar.addSubview(btn)
        
        btn.addTarget(self, action: "ComposeBtn", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    @objc private func ComposeBtn() {
       print("点击了按钮")
        let vc:UIViewController
        if UserAccountViewModel.sharedUserAccount.userLogon {
            vc = ComposeViewController()
        }else{
            vc = OAuthViewController()
        }
        let nav = UINavigationController(rootViewController: vc)
        presentViewController(nav, animated: true, completion: nil)
        
    }
    
//    设置"+"的fram
    private func setupButton() {
        let count = childViewControllers.count
        let width = tabBar.bounds.width / CGFloat (count)
        let rect = CGRect(x: 0, y: 0, width: width, height: tabBar.bounds.height)
        addButton.frame = CGRectOffset(rect, 2*width, 0)
    }
    
    
     

}
